// import { Component } from '@angular/core';
import {IonicPage,AlertController, NavController,Platform,ToastController} from 'ionic-angular';

import {Component,  ViewChild} from '@angular/core';
import { BrowserInterceptor } from '../../service/http-service/browser-interceptor';
import { Slides } from 'ionic-angular';//注入轮播
// import { ListvidoPage } from '../listvido/listvido';
import {ApplicantService} from "../../service/http-service/applicant.service";

import {VideoListService} from "../../service/http-service/getvideolist.service";
// import {PhonePage} from "../phone/phone";
// import {MePage} from "../me/me";
// import {MystatePage} from "../mystate/mystate";
// import { IonicPage } from 'ionic-angular';
import {UsrLogin} from "../../service/http-service/usrLogin.service";
import {ValidateUtils} from "../../util/validate-utils";



//import {TabHomePage} from '../tab-home-page/tab-home-page';

@IonicPage(
  {
    name: 'HomePage',
    segment: 'home'
  }

)
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
// export class HomePage implements OnInit{
export class HomePage{

  display: string;
  video_datas: any;
  token: any;
  openid:any;
  phone: number;
  page: any;
  currentPage:any;
  lastPage:any;
  scrollSign: any;
  timeStamp2: number;
  authenSign:boolean;
  number1: any;



  sliders: Array<{img: string, name: string, type: string, depict: string,id:number,url:string}>;
  dir: string = 'ltr';
  @ViewChild('mySlider') slider1:Slides;
  // mySlideOptions={
  //   autoplay:2000,
  //   initialSlide:0,
  //   pager:true,
  //   loop:true,
  //   speed:300
  // };


  constructor(public toastCtrl:ToastController,public usrLogin: UsrLogin, public navCtrl: NavController,public validate:ValidateUtils,public alertCtrl:AlertController,
              public videoListService:VideoListService,public platform: Platform,public bint: BrowserInterceptor,public applicationService:ApplicantService) {
    this.dir = platform.dir();
    console.log(this.dir);

  }

  ionViewDidLoad(){


    var dev = false;//dev true:测试环境 false:线上环境




    if(dev){

      let options1 = {"id":11,"realname":null,"gender":0,"age":null,"phone":13910296297,"openid":"otjnQwZG7qciTi-K21SmWkN-1VB8","password":null,"header_img":"http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIEHafTGHpmIGJLSUrg7RuXXrN3wGezFgfbLXUbKIBLy63YflqKSgTE1cT6BeUmQQEP0oBertquqA/0","gmtCreate":1513915509000,"gmtModified":1513915509000,"username":"李德","token":"Bear_eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMSIsImlhdCI6MTUyNjAxODA5MCwic3ViIjoie1wiaWRcIjoxMSxcIm9wZW5pZFwiOlwib3RqblF3Wkc3cWNpVGktSzIxU21Xa04tMVZCOFwifSIsImlzcyI6Imh1aXpoaWhhbmxpbiIsImV4cCI6MTUyNjYyMjg5MH0.8V-MSbQ3g7djG6P1njbYyAwGi_HnpD7RLBmZF936OSMmAlschc","role":{"id":11,"auth":null,"name":null,"permis":null,"gmtCreate":1513915509000,"gmtModified":1513915509000},"realName":null}

      localStorage.setItem('options', JSON.stringify(options1));
      localStorage.setItem('phone',JSON.parse(localStorage.getItem('options')).phone);
      localStorage.setItem('token',JSON.parse(localStorage.getItem('options')).token);
      localStorage.setItem('id',JSON.parse(localStorage.getItem('options')).id);

    }else{
      let useragent = navigator.userAgent;
      this.bint.browserBlock(useragent);
      this.usrLogin.authenJudge();

      //   this.timeStamp2 = Number(localStorage.getItem("timeStamp2"));
      // // console.log("this.timeStamp0");
      // // console.log(this.timeStamp1);
      // this.token = localStorage.getItem("token");
      // // console.log("this.timeStamp0");
      // // console.log(this.timeStamp1);
      // console.log(this.timeStamp2!=null&&this.timeStamp2!=undefined&&this.timeStamp2!=0);
      //
      // if(this.token==null||this.token==undefined){
      //   this.authenSign=true;
      // } else {
      //
      //   if (this.timeStamp2 != null && this.timeStamp2 != undefined && this.timeStamp2 != 0) {
      //     this.timeStamp2 = Math.floor(((new Date().getTime()) - this.timeStamp2) / (24 * 3600 * 1000));
      //     if (this.timeStamp2 >= 10 || this.timeStamp2 == NaN || this.timeStamp2 < 0) {
      //       console.log("this.timeStamp1")
      //       console.log(this.timeStamp2);
      //       this.authenSign = true;
      //     } else {
      //       console.log("this.timeStamp2")
      //       this.authenSign = false;
      //       console.log("9999999999999999999999");
      //       this.applicationService.freshTokenn();
      //     }
      //     console.log(this.authenSign);
      //   } else {
      //     console.log("this.timeStamp3")
      //     this.authenSign = true
      //   }
      // }
      //
      // if(this.authenSign==true){
      //   console.log("this.timeStamp4")
      //   //   console.log(this.authenSign);
      //   // this.number1=Number(localStorage.getItem("aaaa"));
      //   // this.number1++;
      //   //  console.log(this.number1);
      //   // localStorage.setItem("aaaa",this.number1);
      //   //  if(this.number1<2){}
      //   this.usrLogin.goAuth();
      //
      //  }

    }
      // console.log(this.token);
      // console.log(this.openid);

    this.load();
    this.dofresh();

    // this.phone = Number(localStorage.getItem("phone"));
    //
    // if(isNaN(this.phone)){
    //   console.log("绑定手机号");
    //   this.display = "绑定手机号"
    // } else {
    //
    //   if (this.phone != null && this.phone != undefined && this.phone != 0) {
    //     console.log("1111111111111111")
    //     console.log(this.phone)
    //     console.log("个人中心");
    //     this.display = "个人中心";
    //   } else {
    //
    //     console.log("绑定手机号");
    //     this.display = "绑定手机号"
    //   }
    //
    // }

  }


ionViewDidEnter(){
  this.phone = Number(localStorage.getItem("phone"));

  if(isNaN(this.phone)){
    console.log("绑定手机号");
    this.display = "绑定手机号"
  } else {

    if (this.phone != null && this.phone != undefined && this.phone != 0) {
      // console.log("1111111111111111")
      // console.log(this.phone)
      // console.log("个人中心");
      this.display = "个人中心";
    } else {

      // console.log("绑定手机号");
      this.display = "绑定手机号"
    }
  }
}

  //  ionViewDidEnter(){
  // // this.navBar.backButtonClick = this.backButtonClick;
  //
  //  console.log('ionViewDidEnter');
  //  if(this.slider1 && this.slider1 != undefined){
  //    this.slider1.autoplayDisableOnInteraction = false;
  //    this.slider1.startAutoplay();
  //  }
  //   }

  ionViewWillEnter(){


    // console.log('ionViewDidEnter');
    if(this.slider1 && this.slider1 != undefined){
      this.slider1.autoplayDisableOnInteraction = false;
      this.slider1.startAutoplay();

    }

  }

  // backButtonClick = (e: UIEvent) => {
  //   // do something
  //   this.navCtrl.pop();
  //   }

  ionViewWillLeave(){

    if(this.slider1 && this.slider1 != undefined){
      this.slider1.stopAutoplay();
    }

  }

  // slideChanged () {
  //   // console.log('slideChanged');
  //   // if(this.bannerSlide && this.bannerSlide != undefined){
  //   //   this.bannerSlide.resize();
  //   //   this.bannerSlide.update();
  //   // }
  // }

  load() {//页面加载完成后自己调用
    // setInterval(()=>{
    //   this.slider1.slideNext(300,true);
    // },2000);




    this.videoListService.getVideoList('campaign/banner').subscribe(resp => {
      // console.log("ddddd")
      if (resp.data !== null) {

        // console.log(resp.data);
        // this.video_datas=resp.data;
        this.sliders = resp.data;
        // console.log(this.sliders[0].img);
        // console.log(this.video_datas[1].img);
        // console.log(this.video_datas[2].img);
      } else {
        console.log("resp.data==null")
      }
    }, error2 => {
      console.log("error");
      console.log(error2);
    });
  }

  //
  //   this.videoListService.postVideoList('campaign/getVideoList').subscribe(resp=>{
  //     console.log("aaaaaa")
  //     if (resp.data!==null){
  //
  //       console.log(resp.data);
  //
  //       this.video_datas=resp.data.list;
  //       // this.sliders=resp.data;
  //       // console.log(this.video_datas.pageSize);
  //       console.log(this.video_datas[0].img);
  //       // console.log(this.video_datas.list[4].img);
  //     }else {
  //       console.log("resp.data==null")
  //     }
  //   },error2 => {
  //     console.log("error");
  //     console.log(error2);
  //   });
  //
  //
  // }

  doRefresh(refresher){
    // console.log('1Begin async operation');
    setTimeout(() => {
      this.dofresh();   //绑定方法
      // console.log('1Async operation has ended');
      refresher.complete();
    }, 1000);
  }

  doInfinite(infiniteScroll) {
    // console.log('Begin async operation');

    setTimeout(() => {
      this.loadmore();

      // console.log('Async operation has ended');
      infiniteScroll.complete();


    }, 1000);
  }


  // dofresh(listNum){
  //   this.videoListService.postVideoList('campaign/getVideoList').subscribe(resp=>{
  //     // console.log("aaaaaa")
  //     if (resp.data!==null){
  //
  //       // console.log(resp.data);
  //
  //       this.video_datas=resp.data.list.slice(0,listNum);
  //       // this.sliders=resp.data;
  //       // console.log(this.video_datas.pageSize);
  //       // console.log(this.video_datas[0].img);
  //       // console.log(this.video_datas.list[4].img);
  //     }else {
  //       console.log("resp.data==null")
  //     }
  //   },error2 => {
  //     console.log("error");
  //     console.log(error2);
  //   });
  //
  // }

  dofresh(){
    this.currentPage=1;
    this.scrollSign=true;
    this.videoListService.postVideoList(this.currentPage).subscribe(resp=>{

      // console.log("aaaaaa")
      if (resp.data!==null){

        // console.log(resp.data);


        this.video_datas=resp.data.list;
        this.lastPage=resp.data.isLastPage;

        // this.sliders=resp.data;
        console.log(resp.data);
        // console.log(this.video_datas[0].img);
        // console.log(this.video_datas.list[4].img);
      }else {
        console.log("resp.data==null")
      }
    },error2 => {
      console.log("error");
      console.log(error2);
    });

  }

  loadmore(){


    if(this.lastPage){
      if(this.scrollSign){
        this.validate.showtoast('敬请关注，更多视频上传中......',this.toastCtrl,'bottom');

      this.scrollSign=false;
      }
      else{this.scrollSign=true}

    } else {
      ++this.currentPage;
    this.videoListService.postVideoList(this.currentPage).subscribe(resp=>{

      // console.log("aaaaaa")
      if (resp.data!==null){

        // console.log(resp.data);
        console.log(resp.data);
        this.lastPage=resp.data.isLastPage;

        this.video_datas=this.video_datas.concat(resp.data.list);

        // this.sliders=resp.data;
        // console.log(this.video_datas);
        // console.log(this.video_datas[0].img);
        // console.log(this.video_datas.list[4].img);
      }else {
        console.log("resp.data==null")
      }
    },error2 => {
      console.log("error");
      console.log(error2);
    });
    }

  }


  /**跳转*/
  // tiao(page){
  //   this.navCtrl.push(page);
  //   console.log("ddd");
  //  // }
  // new (videoId:number,videoUrl:string,name:string,descr:string){
  //
  //   this.navCtrl.push(ListvidoPage,{Id:videoId,Url:videoUrl,Name:name,Descr:descr});
  //
  //  }
  new (videoId:number){

    this.navCtrl.push('ListvidoPage',{'Id':videoId});
  }




  loginPcenter(){


    if (this.display =="绑定手机号") {
     this.navCtrl.push('PhonePage');

    } else if(this.display=="个人中心") {
     this.navCtrl.push('MePage');
    }erro4=>{ console.log(erro4)};

  };

}
